package com.thread.wait;

/**
 * @author wangyingjie
 * @date
 * @remark
 */
public class BadLockOnInteger implements Runnable {
    private static Integer i = 0;
    static BadLockOnInteger obj = new BadLockOnInteger();
    @Override
    public void run() {
        for (int j = 0; j < 10000; j++) {
            //因为integer是不可变对象final修饰的，每次++都是新建一个对象再赋值，所以锁不住，要用同一个对象锁才可以
            synchronized (this){
                i++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(obj);
        Thread t2 = new Thread(obj);
        t1.start();
        t2.start();
        t1.join();t2.join();
        System.out.println(i);
    }
}
